package com.mikhailenko.transportMVC.controller;

import java.io.File;

import javax.json.JsonObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.mikhailenko.transportMVC.json.MyJsonWriter;
import com.mikhailenko.transportMVC.model.JsonEntity;
import com.mikhailenko.transportMVC.model.es.InputEntityFake;
import com.mikhailenko.transportMVC.service.es.InputFakeService;

@Controller
@RequestMapping(value = "/input")
public class InputController {

    @Autowired
    private InputFakeService inputService;

//    private InitialContext initialContext;

//     @EJB
//     private InputEjb inputBean;
     
//     @Autowired
     private MyJsonWriter jsonWriter;

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView newInputPage() {
        ModelAndView mav = new ModelAndView("es/input-new", "input", new InputEntityFake());
        return mav;
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ModelAndView newInputSave(@ModelAttribute("input") InputEntityFake input,
            RedirectAttributes redirectAttributes) {

        ModelAndView mav = new ModelAndView();

        mav.setViewName("redirect:/input/create");
        input.setUsed(false);
        String message = "Input: " + input.getName() + " was created successfully";
        inputService.create(input);
        redirectAttributes.addFlashAttribute("message", message);
        return mav;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView getAllInputs() {
        ModelAndView mav = new ModelAndView("es/input-list");
        
        jsonWriter = new MyJsonWriter();
        JsonObject json = jsonWriter.createJson();
        
        File file = new File("D:\\json.json");
        jsonWriter.writeJson(json, file);

        File marshallFile = new File("D:\\marshall.json");
        
        JsonEntity entity = new JsonEntity();
        entity.setId(0);
        entity.setName("Json Entity");
        
        jsonWriter.marshall(entity, marshallFile);
        
//        InputEjb inputBean = null;
//
//        try {
//            initialContext = new InitialContext();
//            inputBean = (InputEjb) initialContext.lookup("ejb/inputEjbBean");
//        } catch (NamingException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        List<InputEntity> inputs = inputBean.getAll();
//        mav.addObject("inputList", inputs);

        return mav;
    }
    // TODO: finish next methods: list of all inputs, update, delete
    
    

}
